package com.example.demo;

import org.springframework.boot.context.event.ApplicationFailedEvent;
import org.springframework.boot.context.event.ApplicationStartedEvent;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;

/**
 * 监听项目运行状态
 */
@Component
public class StaredListener implements ApplicationListener<ApplicationEvent> {

    /**
     * 监听事件发生
     *
     * @param event 事件信息
     */
    @Override
    public void onApplicationEvent(ApplicationEvent event) {
        if (event instanceof ApplicationStartedEvent) {
            System.out.println("\n\n\n服务启动成功.........\n\n\n");
        }
        if (event instanceof ApplicationFailedEvent) {
            ApplicationFailedEvent failedEvent = (ApplicationFailedEvent) event;
            Throwable exception = failedEvent.getException();
            System.out.println("\n\n\n服务启动失败.........\n\n\n");
            exception.printStackTrace();
        }
    }
}
